# Paper: Is it still the economy? Economic voting in polarized politics
# Individual-level simulation
# Replication: Figure S13 (Supplementary Material)
remove(list = ls())

library(conflicted)
library(tidyverse)
library(dplyr)
conflict_prefer("filter","dplyr")
library(ggplot2)
library(here) # When we have an R-project, it reads files without setting an directory
conflict_prefer("here", "here")
library(nnet)
library(ggeffects)
library(MASS)
conflict_prefer("select","dplyr")

load(here("Final_Paper", "R&R", "IndLevel.RData"))
glimpse(model_data)
table(model_data$partisan)

inpart <- model_data %>% 
  filter(partisan == "Inpart")

summary(ml_inp <- multinom(vote_inc_choice ~ econ_eval*percep_polar + 
                             female + black + hispanic + asian + other_race + 
                             log_age + college + income + state_fips + 
                             election, data = inpart))

ml_inp2 <- ml_inp %>% 
  ggeffect(terms = c("econ_eval", "percep_polar[0.1, 0.5, 1]"), 
           ci.lvl = 0.95)
glimpse(ml_inp2)
ml_inp2 <- data.frame(ml_inp2)

ml_inp2 <- ml_inp2 %>% 
  mutate(partisan = "Inpartisan")


outpart <- model_data %>% 
  filter(partisan == "Outpart")

summary(ml_out <- multinom(vote_inc_choice ~ econ_eval*percep_polar + 
                             female + black + hispanic + asian + other_race + 
                             log_age + college + income + state_fips + 
                             election, data = outpart))

ml_out2 <- ml_out %>% 
  ggeffect(terms = c("econ_eval", "percep_polar[0.1, 0.5, 1]"), 
           ci.lvl = 0.95)
glimpse(ml_out2)
ml_out2 <- data.frame(ml_out2)

ml_out2 <- ml_out2 %>% 
  mutate(partisan = "Outpartisan")



indpart <- model_data %>% 
  filter(partisan == "Ind")

summary(ml_ind <- multinom(vote_inc_choice ~ econ_eval*percep_polar + 
                             female + black + hispanic + asian + other_race + 
                             log_age + college + income + state_fips + 
                             election, data = indpart))

ml_ind2 <- ml_ind %>% 
  ggeffect(terms = c("econ_eval", "percep_polar[0.1, 0.5, 1]"), 
           ci.lvl = 0.95)
glimpse(ml_ind2)
ml_ind2 <- data.frame(ml_ind2)

ml_ind2 <- ml_ind2 %>% 
  mutate(partisan = "Independent")

linpart <- model_data %>% 
  filter(partisan == "LeanIn")

summary(ml_linp <- multinom(vote_inc_choice ~ econ_eval*percep_polar + 
                              female + black + hispanic + asian + other_race + 
                              log_age + college + income + state_fips + 
                              election, data = linpart))

ml_linp2 <- ml_linp %>% 
  ggeffect(terms = c("econ_eval", "percep_polar[0.1, 0.5, 1]"), 
           ci.lvl = 0.95)
glimpse(ml_linp2)
ml_linp2 <- data.frame(ml_linp2)

ml_linp2 <- ml_linp2 %>% 
  mutate(partisan = "Lean Inpartisan")


loutpart <- model_data %>% 
  filter(partisan == "LeanOut")

summary(ml_lout <- multinom(vote_inc_choice ~ econ_eval*percep_polar + 
                              female + black + hispanic + asian + other_race + 
                              log_age + college + income + state_fips + 
                              election, data = loutpart))

ml_lout2 <- ml_lout %>% 
  ggeffect(terms = c("econ_eval", "percep_polar[0.1, 0.5, 1]"), 
           ci.lvl = 0.95)
glimpse(ml_lout2)
ml_lout2 <- data.frame(ml_lout2)

ml_lout2 <- ml_lout2 %>% 
  mutate(partisan = "Lean Outpartisan")


ml_data <- rbind(ml_inp2, ml_out2, ml_ind2, ml_linp2, ml_lout2)
glimpse(ml_data)

table(ml_data$partisan)
g_account <- ml_data %>% 
  mutate(partisan = factor(partisan,
                           levels = c("Inpartisan",
                                      "Lean Inpartisan",
                                      "Independent",
                                      "Lean Outpartisan",
                                      "Outpartisan"),
                           labels = c("President's Party", 
                                      "Lean President's Party",
                                      "Independent",
                                      "Lean Opposing Party",
                                      "Opposing Party"))) %>%    
  mutate(group = as.factor(group)) %>% 
  #mutate(response.level = as.factor(response.level)) %>%
  filter(x == 1) %>% 
  mutate(x = as.factor(x)) %>%
  filter(response.level == "Inc") %>% 
  #filter(response.level == "Inc") %>% 
  #filter(group == 0.33 | group == 1) %>% 
  ggplot(aes(shape = group)) + 
  #geom_hline(yintercept = 0, colour = "black", lty = 2, size = .75) +
  geom_pointrange(aes(x = partisan, y = predicted, 
                      ymin = conf.low, ymax = conf.high),
                  lwd = .85, position = position_dodge(width = .5),
                  color = "darkslategray") + 
  theme_light() + 
  labs(x = NULL, y = "Probability of Voting for the Incumbent Party",
       title = "(a) Voting for the Incumbent Party") +
  scale_shape_manual(name = "Ideological gap:",
                     labels = c("0.1", "0.5", "1"),
                     values = c(15, 16, 17)) +
  scale_y_continuous(breaks = c(1, 0.8, 0.6, 0.4, 0.2, 0),
                     limits = c(0,1)) +
  theme(axis.text.x = element_text(hjust = 0.5, size = 10),
        axis.text.y = element_text(size = 10),
        axis.title.y = element_text(size = 12),
        legend.text = element_text(size = 11),
        legend.title = element_text(size = 11),
        legend.position = "none",
        legend.direction = "horizontal",
        plot.title = element_text(size = 15))
g_account

g_opp <- ml_data %>% 
  mutate(partisan = factor(partisan,
                           levels = c("Inpartisan",
                                      "Lean Inpartisan",
                                      "Independent",
                                      "Lean Outpartisan",
                                      "Outpartisan"),
                           labels = c("President's Party", 
                                      "Lean President's Party",
                                      "Independent",
                                      "Lean Opposing Party",
                                      "Opposing Party"))) %>%   
  mutate(group = as.factor(group)) %>% 
  #mutate(response.level = as.factor(response.level)) %>%
  filter(x == 1) %>% 
  mutate(x = as.factor(x)) %>%
  filter(response.level == "Ops") %>% 
  #filter(response.level == "Inc") %>% 
  #filter(group == 0.33 | group == 1) %>% 
  ggplot(aes(shape = group)) + 
  #geom_hline(yintercept = 0, colour = "black", lty = 2, size = .75) +
  geom_pointrange(aes(x = partisan, y = predicted, 
                      ymin = conf.low, ymax = conf.high),
                  lwd = .85, position = position_dodge(width = .5),
                  color = "darkslategray") + 
  theme_light() + 
  labs(x = NULL, y = "Probability of Voting for the Opposition",
       title = "(b) Voting for the Opposition") +
  scale_shape_manual(name = "Ideological gap:",
                     labels = c("0.1", "0.5", "1"),
                     values = c(15, 16, 17)) +
  scale_y_continuous(breaks = c(1, 0.8, 0.6, 0.4, 0.2, 0),
                     limits = c(0,1)) +
  theme(axis.text.x = element_text(hjust = 0.5, size = 10),
        axis.text.y = element_text(size = 10),
        axis.title.y = element_text(size = 12),
        legend.text = element_text(size = 11),
        legend.title = element_text(size = 11),
        legend.position = "none",
        legend.direction = "horizontal",
        plot.title = element_text(size = 15))
g_opp


g_abs <- ml_data %>% 
  mutate(partisan = factor(partisan,
                           levels = c("Inpartisan",
                                      "Lean Inpartisan",
                                      "Independent",
                                      "Lean Outpartisan",
                                      "Outpartisan"),
                           labels = c("President's Party", 
                                      "Lean President's Party",
                                      "Independent",
                                      "Lean Opposing Party",
                                      "Opposing Party"))) %>%  
  mutate(group = as.factor(group)) %>% 
  #mutate(response.level = as.factor(response.level)) %>%
  filter(x == 1) %>% 
  mutate(x = as.factor(x)) %>%
  filter(response.level == "Abs") %>% 
  #filter(response.level == "Inc") %>% 
  #filter(group == 0.33 | group == 1) %>% 
  ggplot(aes(shape = group)) + 
  #geom_hline(yintercept = 0, colour = "black", lty = 2, size = .75) +
  geom_pointrange(aes(x = partisan, y = predicted, 
                      ymin = conf.low, ymax = conf.high),
                  lwd = .85, position = position_dodge(width = .5),
                  color = "darkslategray") + 
  theme_light() + 
  labs(x = "Partisan Attachment", y = "Probability of Abstention",
       title = "(c) Abstention") +
  scale_shape_manual(name = "Ideological gap:",
                     labels = c("0.1", "0.5", "1"),
                     values = c(15, 16, 17)) +
  scale_y_continuous(breaks = c(1, 0.8, 0.6, 0.4, 0.2, 0),
                     limits = c(0,1)) +
  theme(axis.text.x = element_text(hjust = 0.5, size = 10),
        axis.text.y = element_text(size = 10),
        axis.title.y = element_text(size = 12),
        axis.title.x = element_text(size = 12),
        legend.text = element_text(size = 13),
        legend.title = element_text(size = 12),
        legend.position = "bottom",
        legend.direction = "horizontal",
        plot.title = element_text(size = 15))
g_abs

multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) {
  library(grid)
  # Make a list from the ... arguments and plotlist
  plots <- c(list(...), plotlist)
  numPlots = length(plots)
  # If layout is NULL, then use 'cols' to determine layout
  if (is.null(layout)) {
    # Make the panel
    # ncol: Number of columns of plots
    # nrow: Number of rows needed, calculated from # of cols
    layout <- matrix(seq(1, cols * ceiling(numPlots/cols)),
                     ncol = cols, nrow = ceiling(numPlots/cols))
  }
  if (numPlots==1) {
    print(plots[[1]])
  } else {
    # Set up the page
    grid.newpage()
    pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))
    # Make each plot, in the correct location
    for (i in 1:numPlots) {
      # Get the i,j matrix positions of the regions that contain this subplot
      matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE))
      print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row,
                                      layout.pos.col = matchidx$col))
    }
  }
}

multiplot(g_account, g_abs, g_opp, cols = 2)
